package front

import (
	"errors"
	"hft/Database/admin"
	"hft/Database/admin/country_setting"
	"hft/Database/admin/home_swiper"
	"hft/Http/Models/response"
	"hft/Http/Services/new_clue/clue_utils"
	"hft/e"
	"hft/global"
)

type Index struct{}

func (*Index) GetIndex(city int) (info *response.Index) {

	var swiper []*home_swiper.HomeSwiper
	var priceRange []*admin.Price
	var feature []*admin.LouPanTeSe
	var articleCategory []*admin.ArticleType
	var FriendlyLink []*admin.FriendlyLink
	var sellStatus []*admin.SellStatus
	var rooms []*admin.RoomType
	var areas []*admin.Area
	var properties []*admin.Property
	var TotalPrice []*admin.TotalPrice
	var types []*admin.ActiveGroup
	var activities []*admin.CommonActivity
	var region []*admin.Region
	var emoji []*country_setting.Emoji

	global.OC_DB.Where("city = ?", city).Find(&swiper)
	global.OC_DB.Where("city = ?", city).Order("sort desc").Find(&priceRange)
	global.OC_DB.Where("city = ?", city).Order("sort desc").Find(&TotalPrice)
	global.OC_DB.Order("sort desc").Find(&feature)
	global.OC_DB.Order("sort desc").Find(&articleCategory)
	global.OC_DB.Order("sort desc").Find(&FriendlyLink)
	global.OC_DB.Order("sort desc").Find(&rooms)
	global.OC_DB.Order("sort desc").Find(&areas)
	global.OC_DB.Order("sort desc").Find(&properties)
	global.OC_DB.Order("sort desc").Find(&types)
	global.OC_DB.Order("sort desc").Where("level = ?", "province").Preload("Districts").Find(&region)
	global.OC_DB.Order("sort desc").Find(&emoji)
	global.OC_DB.Find(&activities)
	global.OC_DB.Find(&sellStatus)

	var newSwiper = make([]*response.SwiperItem, 0)
	var newPriceRange = make([]*response.Price, 0)
	var newTotalPriceRange = make([]*response.TotalPrice, 0)
	var newFeature = make([]*response.BuildingFeature, 0)
	var newArcCategory = make([]*response.ArticleCategory, 0)
	var newFriendLink = make([]*response.FriendLink, 0)
	var newStatus = make([]*response.SellStatus, 0)
	var newActivities = make([]*response.GlobalActivities, 0)
	var H5Swiper = make([]*response.SwiperItem, 0)
	var AppSwiper = make([]*response.SwiperItem, 0)
	var newRegion = make([]*response.Region, 0)
	for _, v := range swiper {
		if v.Type == 0 {
			newSwiper = append(newSwiper, &response.SwiperItem{Url: v.Link, Cover: v.Cover})
		} else if v.Type == 1 {
			H5Swiper = append(H5Swiper, &response.SwiperItem{Url: v.Link, Cover: v.Cover})
		} else if v.Type == 3 {
			AppSwiper = append(AppSwiper, &response.SwiperItem{Url: v.Link, Cover: v.Cover})
		}

	}
	for _, v := range priceRange {
		newPriceRange = append(newPriceRange, &response.Price{PriceType: v.PriceType, Min: v.Min, Max: v.Max})
	}
	for _, v := range TotalPrice {
		newTotalPriceRange = append(newTotalPriceRange, &response.TotalPrice{Min: v.Min, Max: v.Max, PriceType: v.PriceType})
	}
	for _, v := range feature {
		newFeature = append(newFeature, &response.BuildingFeature{Name: v.Name, ID: int(v.ID), IsShowSearch: v.ShowInSearch})
	}
	for _, v := range articleCategory {
		newArcCategory = append(newArcCategory, &response.ArticleCategory{ID: int(v.ID), Name: v.Name})
	}
	for _, v := range FriendlyLink {
		newFriendLink = append(newFriendLink, &response.FriendLink{Name: v.Name, Url: v.Link})
	}
	for _, v := range sellStatus {
		newStatus = append(newStatus, &response.SellStatus{Name: v.Name, BackColor: v.BackColor, TextColor: v.TextColor, StatusCode: v.StatusCode})
	}
	for _, v := range region {
		var name string
		if v.Alias != "" {
			name = v.Alias
		} else {
			name = v.Name
		}
		var newRegionItem = &response.Region{
			ID:        v.ID,
			Name:      v.Name,
			Location:  v.Center,
			FirstWord: v.FirstWord,
			RealName:  name,
		}
		for _, j := range v.Districts {
			var name1 string
			if j.Alias != "" {
				name1 = j.Alias
			} else {
				name1 = j.Name
			}
			newRegionItem.Children = append(newRegionItem.Children, &response.Region{
				ID:       j.ID,
				Name:     j.Name,
				Location: j.Center,
				RealName: name1,
				PName:    v.Name,
			})
		}
		newRegion = append(newRegion, newRegionItem)
	}

	for _, v := range activities {
		var cover string
		var button_logo string
		var out_size_cover string
		if v.Cover != "" {
			cover = global.OC_CONFIG.AliyunOSS.BucketUrl + v.Cover
		}
		if v.ButtonLogo != "" {
			button_logo = global.OC_CONFIG.AliyunOSS.BucketUrl + v.ButtonLogo
		}
		if v.OutSizeCover != "" {
			out_size_cover = global.OC_CONFIG.AliyunOSS.BucketUrl + v.OutSizeCover
		}
		newActivities = append(newActivities, &response.GlobalActivities{
			ID:               uint(v.Code),
			Cont:             v.Cont,
			Cover:            cover,
			Title:            v.Title,
			ButtonName:       v.ButtonName,
			ButtonColor:      v.ButtonColor,
			ButtonLogo:       button_logo,
			ButtonBottomName: v.ButtonBottomName,
			OutSizeCover:     out_size_cover,
			SuccessButton:    v.SuccessButton,
		})
	}

	// key, _ := tools.EnPwdCode([]byte(global.OC_CONFIG.Amap.Key))
	var countrySet *admin.CountrySetting
	var citySet *admin.CitySetting
	global.OC_DB.First(&countrySet, 1)
	global.OC_DB.Where("region = ?", city).First(&citySet)
	var index = &response.Index{
		Residential:     citySet.Residential,
		Commercial:      citySet.Commercial,
		CityBaseFund:    citySet.GongJiJIn,
		Lpr:             countrySet.Lpr,
		BaseFund:        countrySet.BaseFund,
		Swiper:          newSwiper,
		H5Swiper:        H5Swiper,
		AppSwiper:       AppSwiper,
		PriceRange:      newPriceRange,
		BuildingFeature: newFeature,
		ArticleCategory: newArcCategory,
		FriendLink:      newFriendLink,
		SellStatus:      newStatus,
		ImgPrefix:       global.OC_CONFIG.AliyunOSS.BucketUrl,
		RoomTypes:       rooms,
		AreaList:        areas,
		Properties:      properties,
		TotalPrice:      newTotalPriceRange,
		ActiveTypes:     types,
		Activities:      newActivities,
		Regions:         newRegion,
		Emoji:           emoji,
	}

	return index
}

func (*Index) SEOList() (data []*response.Route) {
	var routes []*admin.ShopRoute
	var newRoutes = make([]*response.Route, 0)
	global.OC_DB.Order("sort desc").Find(&routes)
	for _, v := range routes {
		newRoutes = append(newRoutes, &response.Route{Name: v.Name, Tag: v.RouteName, Title: v.Title, Keywords: v.Keywords, Description: v.Description})
	}

	return newRoutes
}

/**检查登录状态**/
func (*Index) CheckLoginStatus(user_token string) (uuid string, err error) {
	if claims, ok := clue_utils.Jwt(user_token); !ok {
		// token错误
		return "", errors.New(e.ErrorInfo)
	} else {
		return claims.UUID, nil
	}
}

var IndexService = new(Index)
